package org.hadoop.spark;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ForUpdateStateByKey {
    public static void main(String[] args) throws Exception {
        ServerSocket ss = new ServerSocket(9999);
        while (true) {
            System.out.println("等待连接...");
            Socket client = ss.accept();
            System.out.println("连接，启动线程..." + client);
            new MyThread(client).start();
        }
    }
    /**
     * 使用以下线程，向连接成功的客户端发送数据,第一秒发送一次相同的数据
     **/
    public static class MyThread extends Thread {
        private Socket client;

        public MyThread(Socket client) {
            this.client = client;
        }
        @Override
        public void run() {
            try {
                int times = 1;
                PrintStream ps = new PrintStream(client.getOutputStream(), true);
                while (true) {//每一秒向客户端发送一个信息
                    ps.println((times++) + " Jack Mary");
                    Thread.sleep(1000);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}